package com.foxconn.ipebg.common.system.controller;

import ipebg.mzk.facade.entity.MsFunctionOption;
import ipebg.mzk.facade.entity.MsFunctionRoleOption;
import ipebg.mzk.facade.system.service.MsFunctionOptionService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;


/**
 * 對菜單按鈕操作方法
 * @author F3225975
 *
 */
@Controller
@RequestMapping("/functionoption.do")
public class FunctionOptionController {
	 
	 @Resource(name="msFunctionOptionServiceImpl")
	 MsFunctionOptionService functionOptionService;
	
	/**
	 * 查詢菜單按鈕清單
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(params = "action=list")
	public void optionList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String functionId = request.getParameter("functionId");
		try {
			List<MsFunctionOption> optionList = (List<MsFunctionOption>) functionOptionService.findAllOptionByFunc(functionId);
			
			Map objMap = new HashMap();
			objMap.put("rows", optionList);
			objMap.put("total", optionList.size());
			
			response.getWriter().write(JSON.toJSONString(objMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增菜單按鈕功能
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=add")
	public void add(HttpServletRequest request, HttpServletResponse response, Model model) {
		String functionId = request.getParameter("functionId");
		String optionCode = request.getParameter("optionCode");
		String optionName = request.getParameter("optionName");
		
		MsFunctionOption optionBean = new MsFunctionOption();
		optionBean.setFunctionId(functionId);
		optionBean.setOptionId(optionCode);
		optionBean.setOptionDsc(optionName);
		try {
			functionOptionService.save(optionBean);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 刪除菜单按钮功能
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=delete")
	public void delete(HttpServletRequest request, HttpServletResponse response, Model model) {
		String functionId = request.getParameter("functionId");
		String optionId = request.getParameter("optionId");
		
		try {
			List<MsFunctionRoleOption> functionRoleOptionList = functionOptionService.findFunctionRoleOptionListByOptionId(optionId);
			if(functionRoleOptionList.size() > 0){
				response.getWriter().write("used");
			}else{
				MsFunctionOption option = functionOptionService.findOption(functionId,optionId);
				functionOptionService.deleteOption(option);
				response.getWriter().write("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
